package com.authine.cloudpivot.ext.utils;

import com.alibaba.cola.dto.RequestContext;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @program: gm-market
 * @description:
 * @author: wlw
 * @create: 2021-08-21 09:58
 **/
@Slf4j
public class RestTemplateUtil {
    public static RestTemplate restTemplate = initRestTemplate();

    public static Map rest(String url, Object data, HttpMethod method) {
        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.APPLICATION_JSON);
        header.add("corpLoginId", RequestContext.getCorpLoginId());
        header.add("loginId", RequestContext.getLoginId());
        header.add("loginName", RequestContext.getLoginName());
        Map bodyParam = null;
        if(data instanceof Map){
             bodyParam = (Map)data;
        }else {
            try {
                if (Objects.nonNull(data)) {
                    bodyParam = BoUtils.bo2Map(data);
                }
            } catch (Exception e) {
                log.error("请求数据转换异常", e);
            }
        }
        HttpEntity httpEntity = new HttpEntity<>(bodyParam, header);
        log.info("request url:{},param:{}",url,bodyParam);
        Map body = restTemplate.exchange(url, method, httpEntity, Map.class).getBody();
        log.info("response body:{}",body);
        return body;
    }
    public static Map restArray(String url, Collection data, HttpMethod method){
        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.APPLICATION_JSON);
        header.add("corpLoginId", RequestContext.getCorpLoginId());
        header.add("loginId", RequestContext.getLoginId());
        header.add("loginName", RequestContext.getLoginName());
        HttpEntity httpEntity = new HttpEntity<>(JSON.toJSON(data), header);
        log.info("请求地址：{},请求参数：{}",url,data);
        return restTemplate.exchange(url, method, httpEntity, Map.class).getBody();
    }


    private synchronized static RestTemplate initRestTemplate() {

        HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectionRequestTimeout(15000);
        clientHttpRequestFactory.setReadTimeout(15000);
        RestTemplate template = new RestTemplate(clientHttpRequestFactory);

        List<HttpMessageConverter<?>> converters = template.getMessageConverters();
        converters.add(new ByteArrayHttpMessageConverter());

        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);


        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        jackson2HttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
        jackson2HttpMessageConverter.setPrettyPrint(true);
        converters.add(jackson2HttpMessageConverter);

        for (HttpMessageConverter converter : converters) {
            if (converter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8);
                break;
            }
        }
        template.setMessageConverters(converters);
        restTemplate = template;
        return restTemplate;
    }
}
